package com.example.lancooznbk.view.fragment;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.Toolbar;
import android.text.Html;
import android.text.TextUtils;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.widget.TextView;

import com.example.lancooznbk.R;
import com.example.lancooznbk.model.result.TemplateEntity;
import com.example.lancooznbk.presenter.TemplateSettingPresenter;
import com.example.lancooznbk.view.adapter.TemplateAdapter;
import com.example.lancooznbk.view.base.BaseFragment;
import com.example.lancooznbk.view.base.ILoadDataView;
import com.example.lancooznbk.view.base.ILoadMoreDataView;
import com.example.lancooznbk.view.view_interface.ITemplateSettingView;
import com.example.lancooznbk.view.widget.EmptyMainLayout;
import com.example.lancooznbk.view.widget.EmptyViewLayout;
import com.lancoo.reviewsystem.utils.ToastUtils;

import java.util.List;

/**
 * Created by tangminglong on 17/8/30.
 */

public class TemplateSettingFragment extends BaseFragment implements ILoadDataView<List<TemplateEntity>> {

    private TemplateAdapter mAdapter;
    private RecyclerView mRecyclerView;
    private TemplateSettingPresenter mPresenter;
    private TextView mTvIntroduction;

    @Override
    protected void updateViews(boolean isShowLoading) {
        mPresenter.getData(isShowLoading);
    }

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setHasOptionsMenu(true);
    }

    @Override
    public int attachLayoutRes() {
        return R.layout.fragment_template_setting;
    }

    @Override
    public void initViews(View view, Bundle savedInstanceState) {
        Toolbar toolBar = (Toolbar) view.findViewById(R.id.toolbar);
        toolBar.setNavigationIcon(R.mipmap.ic_close);
        initToolBar(toolBar,true,R.string.change_template);
        mTvIntroduction = (TextView) view.findViewById(R.id.tv_template_introduction);
        mAdapter = new TemplateAdapter(getHolderActivity());
        mAdapter.setTemplateCheck(new TemplateAdapter.OnTemplateCheckListener() {
            @Override
            public void onCheck(TemplateEntity templateEntity) {
                setTemplateTips(templateEntity,templateEntity.getTempletName());
            }
        });
        mRecyclerView = (RecyclerView) view.findViewById(R.id.rv);
        mRecyclerView.setLayoutManager(new LinearLayoutManager(getHolderActivity(),LinearLayoutManager.VERTICAL,false));
        mRecyclerView.setAdapter(mAdapter);
        mPresenter = new TemplateSettingPresenter(this);
        mEmptyViewLayout = (EmptyMainLayout) view.findViewById(R.id.container_empty);

    }

    @Override
    public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
        super.onCreateOptionsMenu(menu,inflater);
        inflater.inflate(R.menu.menu_confirm,menu);
        }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        if (item.getItemId() == R.id.menu_confirm){

                mPresenter.updateTemplateSetting(mAdapter.getCheckItem());
                return true;
        }
        return false;
    }

    @Override
    public void loadData(List<TemplateEntity> data) {
        setTemplateTips(data.get(0),data.get(0).getCurTempletName());
        mAdapter.add(data);
    }

    /**
     * 显示当前选中的是哪个模板
     * @param templateEntity
     * @param currentTemplateName
     */
    private void setTemplateTips(TemplateEntity templateEntity,String currentTemplateName){
        String format;
        if(TextUtils.isEmpty(templateEntity.getLastTempletName())){
            format = String.format(getString(R.string.template_introduction),currentTemplateName,templateEntity.getCurTempletName());
        }else {
            format = String.format(getString(R.string.template_introduction),currentTemplateName,templateEntity.getLastTempletName());
        }
        mTvIntroduction.setText(Html.fromHtml(format));
    }


}
